<?php

namespace kdapi;


use app\common\model\OrderReceiptModel;
use app\common\model\StorageModel;

class Eorder extends Api
{
    const ReqURL = 'http://api.kdniao.com/api/Eorderservice';

    public static function order($receipt_id)
    {
        $buildData = self::buildData($receipt_id);
        if (is_array($buildData)) {
            return $buildData;
        }
        $datas = [
            'EBusinessID' => self::EBusinessID,
            'RequestData' => urlencode($buildData),
            'DataType'    => 2,
            'RequestType' => 1007,
        ];
        $datas['DataSign'] = self::encrypt($buildData, self::AppKey);
        $result = self::sendPost(self::ReqURL, $datas);
        return $result;
    }

    public static function buildData($id)
    {
        $receipt = OrderReceiptModel::find($id);
        $storage = StorageModel::find($receipt['storage_id']);
        if (empty($storage['express_code'])) {
            return ['code' => 0, 'msg' => $storage['store_name'] . '还未设置发货快递'];
        }

        $settings = cmf_get_option('express_settings');
        if (!key_exists($storage['express_code'], $settings)) {
            return ['code' => 0, 'msg' => $storage['store_name'] . '快递公司未配置'];
        }

        $express_code = $storage['express_code'];
        //构造电子面单提交信息
        $eorder = [
            'CustomerName' => $settings[$express_code]['CustomerName'],
            'CustomerPwd'  => $settings[$express_code]['CustomerPwd'],
            'MonthCode'    => $settings[$express_code]['MonthCode'],
            'SendSite'     => $settings[$express_code]['SendSite'],
            'SendStaff'    => $settings[$express_code]['SendStaff'],
            'ShipperCode'  => $storage['express_code'],
            'OrderCode'    => $receipt['self_order_no'],
            'PayType'      => 1,
            'ExpType'      => 1,
            'Quantity'     => 1,
            'IsNotice'     => 0,
        ];

        $sender = [
            'Name'         => $storage['sender'],
            'Mobile'       => $storage['tel'],
            'ProvinceName' => $storage['province'],
            'CityName'     => $storage['city'],
            'ExpAreaName'  => $storage['area'],
            'Address'      => $storage['send_address']
        ];

        list($province, $city, $area, $address) = explode(' ', $receipt['area_info']);
        $receiver = [
            'Name'         => $receipt['name'],
            'Mobile'       => $receipt['tel'],
            'ProvinceName' => $province,
            'CityName'     => $city,
            'ExpAreaName'  => $area,
            'Address'      => $address
        ];

        $commodity = [
            [
                'GoodsName'     => $receipt['goods_name'],
                'GoodsQuantity' => 1,
                'GoodsWeight'   => $receipt['weight']
            ]
        ];

        $eorder["Sender"] = $sender;
        $eorder["Receiver"] = $receiver;
        $eorder['Commodity'] = $commodity;
        return json_encode($eorder, JSON_UNESCAPED_UNICODE);
    }
}